/**
 * @author Ahammer
 * @date   2009 04
 * @update 2009 06 new parameters ZoomX, ZoomY, newWidth newHeight ZoomOrsize 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;

import java.awt.RenderingHints;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.BorderExtender;
import javax.media.jai.Interpolation;
import javax.media.jai.InterpolationBicubic;
import javax.media.jai.InterpolationBicubic2;
import javax.media.jai.InterpolationBilinear;
import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;

/**
 * This is the main image processing class
 * There exist two approaches:
 * A user defined JAI operator is just called 
 * or:
 * The actual processing is implemented in this class 
 */
public class IqmResizeOperator{ 


  public IqmResizeOperator() {
	  //WARNING: Don't declare fields here
	  //Fields declared here aren't thread safe!   
  }
  /**
   * 
   * @param ParametrBlockJAI pb
   */
  public Object run(ParameterBlockJAI pbJAI){
	  //Object ob = JAI.create("IqmResize".toLowerCase(), pb, null); //geht nicht
	  //daher hier direkt implementiert
	  	PlanarImage pi = (PlanarImage) pbJAI.getSource(0);
	  	float zoomX    = pbJAI.getFloatParameter("ZoomX");
	  	float zoomY    = pbJAI.getFloatParameter("ZoomY");
	  	int newWidth   = pbJAI.getIntParameter("NewWidth");
	  	int newHeight  = pbJAI.getIntParameter("NewHeight");
	  	int optIntP    = pbJAI.getIntParameter("Interpolation");
	  	int zoomOrSize = pbJAI.getIntParameter("ZoomOrsize");
//	  	int numBands  = pi.getData().getNumBands();
//	  	int pixelSize = pi.getColorModel().getPixelSize();
//		String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
        if (zoomOrSize == 1){  //new size values are essential
          	zoomX = (float)newWidth/(float)pi.getWidth();
          	zoomY = (float)newHeight/(float)pi.getHeight();
        }else{ //zoom values are essential
        	//do nothing zoom values already set
        }
				
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(pi);
		pb.add(zoomX);//x scale factor
		pb.add(zoomY);//y scale factor
		pb.add(0.0F);//x translate
		pb.add(0.0F);//y translate
//		if (optIntP == 0) pb.add(new InterpolationNearest());
//		if (optIntP == 1) pb.add(new InterpolationBilinear());
//		if (optIntP == 2) pb.add(new InterpolationBicubic(10));  //???? 1 oder was????
//		if (optIntP == 3) pb.add(new InterpolationBicubic2(10));
		
		if (optIntP == 0) pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
		if (optIntP == 1) pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
		if (optIntP == 2) pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
		if (optIntP == 3) pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2));

		RenderingHints rh = new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY));
		pi = JAI.create("scale", pb, rh);	
				  
	  return pi;
  }
}
